<?php
if (isset($_GET["path"])) {
    $path = $_GET["path"];
}
if (isset($_GET["username"])) {
    $userName = $_GET["username"];
}
include 'process/connect.php';
include 'class/user.php';
include 'process/isloggedon.php';
$defpath = "./../Desktop/Tubes5Progin/Upload/" . $loggedOnUser->getUserName();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<link rel="stylesheet" href="styles/header.css" />
<link rel="stylesheet" href="styles/form.css" />
<link rel="stylesheet" href="styles/inside.css" />
<link rel="stylesheet" href="styles/file.css" />
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <?php
    include 'templates/header.php';
    $pos = strpos($path, $defpath);
    if (strlen(strstr($path, $defpath)) > 0) {
    ?>
        <body>

            <div class="info2">
                Welcome, <?= $userName ?>
            </div>

            <div class="filePanel">

            <?php
            //echo "<select name=\"file\">\n";

            if ($path != $defpath) {
                //$back
                $found = 0;
                for ($i = strlen($path) - 1; $i >= 0; $i--) {
                    //$found++;
                    if (substr($path, $i, 1) == "/") {
                        $found = $i;
                        break;
                    }
                }
                $back = substr($path, 0, $found);
            ?>
                <div class="icon" ><img height="15px"  src="des/back.png"/></div>
                <div class="lst"  onclick="document.location='<?= "userfile.php?username=" . $userName . "&path=" . $back ?>'">
                    back
                </div>

                <div class="detail">


                <?php
                echo "</br>";
                ?>

            </div>

            <?php
            }

            /////////////
            ?>



            <?php
            ///////////////

            foreach (new DirectoryIterator($path) as $file) {

                if ((!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF']))) {
                    //echo "<option>";

                    if (!$file->isDir()) {
            ?>
                        <div class="icon" ><img height="15px" src="des/file.png"/></div>
                        <div class="lst"  onclick="document.location='<?= "process/download.php?file=" . $file->getFilename() . "&path=" . $path ?>'">


                <?php
                        echo $file->getFilename();
//                        echo "last modified : " . date("F d Y H:i:s.", filemtime($path . "/" . $file->getFilename())) . "</br>";
//                        echo "file size : " . filesize($path . "/" . $file->getFilename()) . "kb </br>";
                ?>

                    </div>

                        <div class="lst" style="margin-right: 10px" onclick="document.location='<?= "process/download.php?file=" . $file->getFilename() . "&path=" . $path ?>'">


                <?php
                        echo "download";
                ?>

                    </div>

                        <div class="lst" style="margin-right: 10px" onclick="document.location='<?= "process/remfile.php?file=" . $file->getFilename() . "&path=" . $path ?>'">


                <?php
                        echo "remove";
                ?>

                    </div>        



                    <div class="detail1">


                <?php
                        echo date("F-d-Y,H:i:s", filemtime($path . "/" . $file->getFilename()));
                ?>

                    </div>
                    <div class="detail">


                <?php
                        echo filesize($path . "/" . $file->getFilename()) . "kb </br>";
                ?>

                    </div>
            <?php
                    }
                    //echo ($file->isDir()) ? : $file->getFilename();
                    //echo "</option>\n";
                    else {
            ?>
                        <div class="icon" ><img height="15px"  src="des/folder.png"/></div>
                        <div class="lst"  onclick="document.location='<?= "userfile.php?username=" . $userName . "&path=" . $path . "/" . $file->getFilename() ?>'">


                <?php
                        echo $file->getFilename() . "</br>";
                ?>

                    </div>
                    <div class="lst" style="margin-right: 10px" onclick="document.location='<?= "process/downloaddir.php?file=" . $file->getFilename() . "&path=" . $path ?>'">


                <?php
                        echo "download";
                ?>

                    </div>

                        <div class="lst" style="margin-right: 10px" onclick="document.location='<?= "process/remdir.php?file=" . $file->getFilename() . "&path=" . $path ?>'">


                <?php
                        echo "remove";
                ?>

                    </div>



                    <div class="detail1">


                <?php
                        echo date("F-d-Y,H:i:s", filemtime($path . "/" . $file->getFilename()));
                ?>

                    </div>
                    <div class="detail">


                <?php
                        echo filesize($path . "/" . $file->getFilename()) . "kb </br>";
                ?>

                    </div>

            <?php
                    }
                }
            }
            ?>



            <?php
        } else {
            //echo "</select>\n";
            ?>
            <body
                <div class="info2">
                    Error, Data could not be retrieved or
                    You may be not an authorized person to see this content
                </div>
            </body>
            <?php
        }
            ?>
        </div>
        <div class="info2">
            <a href="process/logout.php">Logout</a>
        </div>
    </body>
</html>
